/**
 * desc：页面复用
 * author：王文军
 * date：  2022/5/9 下午11:47
 */
"use strict";

const Controller = require("egg").Controller;
const mongo = require("../utils/mongo");
const { success, failed } = require("../utils/request");

class PageController extends Controller {
  async getTemplate() {
    const { ctx } = this;
    ctx.body = await mongo().query("page");
  }
  async addTemplate() {
    const { ctx } = this;
    try {
      const info = await mongo().find("page", {
        npmName: ctx.request.body.npmName,
      });
      if (info.length > 0) {
        return (ctx.body = failed("该模板已存在！"));
      }
      const res = await mongo().insert("page", [ctx.request.body]);
      if (res.result.ok) {
        return (ctx.body = success("添加成功！"));
      }
      ctx.body = failed("添加失败，请稍后重试！");
    } catch (e) {
      ctx.body = failed("系统出错，请稍后重试！");
    }
  }
}

module.exports = PageController;
